package com.aplikata.evok.ui;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import org.primefaces.model.TreeNode;

import com.aplikata.constants.Constants;
import com.aplikata.constants.ConstantsMsg;
import com.aplikata.evok.bo.Therapy;
import com.aplikata.evok.service.EvokTherapyService;
import com.aplikata.ui.UIBean;
import com.aplikata.util.FacesUtil;

@ManagedBean(name = "therapyBean")
public class EvokTherapyBean extends UIBean {

	private static final long serialVersionUID = 1L;

	@ManagedProperty(value = "#{evokTherapyService}")
	private EvokTherapyService therapyService;
	private Therapy therapy;
	private Therapy therapyParent;
	private TreeNode rootNode;
	private TreeNode selectedNode;

	@PostConstruct
	public void init() {
		therapy = new Therapy(Constants.COMMON_STATUS_NORMAL);
		therapyParent = new Therapy(Constants.COMMON_STATUS_NORMAL);
	}
	
	public void submit() {
		if(therapyParent.getId().equals(therapy.getId())){
			therapyParent = new Therapy(Constants.COMMON_STATUS_NORMAL);
		}
		
		try {
			therapyService.updateTherapy(therapy);
			success();
			therapy = new Therapy(therapyParent, Constants.COMMON_STATUS_NORMAL);
		} catch (Exception e) {
			FacesUtil.addActionListenerException(e, log);
		}
	}
	
	public void add() {
		if (selectedNode != null) {
			therapyParent = therapy.getParent();
			if(therapyParent == null){
				therapyParent = new Therapy(Constants.COMMON_STATUS_NORMAL);
			}
			therapy = new Therapy( therapyParent, Constants.COMMON_STATUS_NORMAL);
		}
	}

	public void addChild() {
		if (selectedNode != null) {
			therapyParent = (Therapy) selectedNode.getData();
			if(Constants.COMMON_STATUS_STOP == therapyParent.getStatus()){
				FacesUtil.addErrorMessage("dept_addchild_error", new Object[]{therapyParent.getName()});
				return;
			}
			therapy = new Therapy(therapyParent, Constants.COMMON_STATUS_NORMAL);
		}
	}
	
	public void edit() {
		therapy = (Therapy) selectedNode.getData();
		therapyParent = therapy.getParent();

		if (therapyParent == null) {
			therapyParent = new Therapy(Constants.COMMON_STATUS_NORMAL);
		}
	}
	
	public void stop() {
		if(therapyParent.getId().equals(therapy.getId())){
			therapyParent = new Therapy();
		}
		
		try {
			therapy = therapyService.stopTherapy(therapy);
			success();
		} catch (Exception e) {
			FacesUtil.addActionListenerException(e, log);
		}
	}
	
	public void delete(){
		therapy = (Therapy) selectedNode.getData();
		try {
			therapyService.deleteByKey(Therapy.class, therapy.getId());
			success();
		} catch (Exception e) {
			FacesUtil.addActionListenerException(e, log);
		}
	}

	public void active() {
		therapy.setStatus(Constants.COMMON_STATUS_NORMAL);
		 if(therapyParent.getId().equals(therapy.getId())){
			 therapyParent = new Therapy();
		}
		 
		try {
			therapy = therapyService.updateTherapy(therapy);
			success();
		} catch (Exception e) {
			FacesUtil.addActionListenerException(e, log);
		}
	}

	public EvokTherapyService getTherapyService() {
		return therapyService;
	}

	public void setTherapyService(EvokTherapyService therapyService) {
		this.therapyService = therapyService;
	}

	public Therapy getTherapy() {
		return therapy;
	}

	public void setTherapy(Therapy therapy) {
		this.therapy = therapy;
	}

	public Therapy getTherapyParent() {
		return therapyParent;
	}

	public void setTherapyParent(Therapy therapyParent) {
		this.therapyParent = therapyParent;
	}

	public TreeNode getRootNode() {
		if(rootNode == null){
			rootNode = therapyService.getTherapyNodeTree();
		}
		return rootNode;
	}

	public void setRootNode(TreeNode rootNode) {
		this.rootNode = rootNode;
	}

	public TreeNode getSelectedNode() {
		return selectedNode;
	}

	public void setSelectedNode(TreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}

	private void success(){
		FacesUtil.addInfoMessage(ConstantsMsg.INFO_SUCCESS, new Object[] { therapy.getName() });
		rootNode = null;
		rootNode = this.getRootNode();
	}
}
